package com.lldpet.spring.cloud.demo.order.service.listener;

import cn.ygyg.common.utils.JsonUtils;
import com.lldpet.spring.cloud.demo.store.client.channel.StoreSource;
import com.lldpet.spring.cloud.demo.store.client.event.StoreUpdateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.bus.SpringCloudBusClient;
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.context.annotation.Configuration;

/**
 * @desc：StoreUpdateEventListener
 * @author: guanliang.xue
 * @date: 2021/5/11 13:57
 * @version:
 * @since:
 */
@Configuration
@EnableBinding(StoreSource.class)
public class StoreListener {
    // @EnableBinding(StoreSource.class)
    // 绑定 StoreSource 的交换机

    private static final Logger logger = LoggerFactory.getLogger(StoreListener.class);

    @StreamListener(StoreSource.INPUT_CHANNEL)
    public void onStoreUpdateEvent(RemoteApplicationEvent remoteApplicationEvent){
        // 用于区分库存服务中的不同事件类型
        if(remoteApplicationEvent instanceof StoreUpdateEvent){
            logger.info("order-service#StoreListener 监听到 库存变更事件");
            logger.info("StoreListener.storeUpdateEvent: {}", JsonUtils.toText(remoteApplicationEvent));
        }

        // todo 监听到事件后到业务逻辑处理

    }
}
